package org.example.rmsmsapi.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018-09-07.
 */@Configuration
public class DataSourceConfig {

    /**
     * 数据源1
     * spring.datasource.db1 ：application.properteis中对应属性的前缀
     * @return
     */
    @Bean(name = "datasource1")
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    public DataSource dataSource1() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 数据源1
     * spring.datasource.db2 ：application.properteis中对应属性的前缀
     * @return
     */
    @Bean(name = "datasource2")
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource dataSource2() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 动态数据源: 通过AOP在不同数据源之间动态切换
     * @return
     */
    @Primary
    @Bean(name = "dynamicDataSource")
    public DataSource dynamicDataSource() {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        // 默认数据源
        dynamicDataSource.setDefaultTargetDataSource(dataSource1());
        // 配置多数据源
        Map<Object, Object> dsMap = new HashMap();
        dsMap.put("datasource1", dataSource1());
        dsMap.put("datasource2", dataSource2());

        dynamicDataSource.setTargetDataSources(dsMap);
        return dynamicDataSource;
    }

    /**
     * 配置@Transactional注解
     * @return
     */
    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dynamicDataSource());
    }
    /*新增配置*/
    @Component
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public static class AjaxCorsFilter extends CorsFilter {
        public AjaxCorsFilter() {
            super(configurationSource());
        }

        private static UrlBasedCorsConfigurationSource configurationSource() {
            CorsConfiguration corsConfig = new CorsConfiguration();
            List<String> allowedHeaders = Arrays.asList("x-auth-token", "content-type", "X-Requested-With", "XMLHttpRequest");
            List<String> exposedHeaders = Arrays.asList("x-auth-token", "content-type", "X-Requested-With", "XMLHttpRequest");
            List<String> allowedMethods = Arrays.asList("POST", "GET", "DELETE", "PUT", "OPTIONS");
            List<String> allowedOrigins = Arrays.asList("*");
            corsConfig.setAllowedHeaders(allowedHeaders);
            corsConfig.setAllowedMethods(allowedMethods);
            corsConfig.setAllowedOrigins(allowedOrigins);
            corsConfig.setExposedHeaders(exposedHeaders);
            corsConfig.addAllowedOrigin("*"); //允许任何域名
            corsConfig.addAllowedHeader("*"); //允许任何头
            corsConfig.addAllowedMethod("*"); //允许任何方法
            corsConfig.setMaxAge(36000L);
            corsConfig.setAllowCredentials(true);

            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", corsConfig);
            return source;
        }
    }

}
